package com.mycompany.inventory.persistence;

import com.mycompany.common.AbstractDao;
import com.mycompany.common.jdbc.ConnectionDatabase;
import com.mycompany.inventory.pojo.CostingInfo;
import com.mycompany.inventory.pojo.SaleInfo;

import java.util.ArrayList;
import java.util.List;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by My Company.
 * User: loitran
 * Date: Feb 29, 2012
 * Time: 1:53:19 PM
 * com.mycompany.inventory.persistence.
 */
public class SaleInfoDAO extends AbstractDao<SaleInfo>{
	
	private Connection con = this.connect;
	public static final String SELECT_ALL_SQL = "select * from oce_sale_info";
	public static final String SELECT_SQL = "select * from oce_sale_info where saleInfoId = ?";
	public static final String SELECT_BY_PRODUCTID_SQL = "select * from oce_sale_info where product_id = ?";
	public static final String INSERT_SQL = "INSERT INTO oce_sale_info (saleInfoId,product_id,name,saleValue,showed) VALUES (?,?,?,?,?)";
	public static final String UPDATE_SQL = "UPDATE oce_sale_info SET  name = ? , showed = ? ,saleValue = ? WHERE saleInfoId = ?";
	
	public SaleInfo create(SaleInfo obj){
		try{
			PreparedStatement ps = con.prepareStatement(INSERT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);	
			long id = 0l;
			ps.setLong(1,id);
			ps.setLong(2, Long.parseLong(obj.getProductId()));
			ps.setString(3, obj.getName());
			ps.setLong(4, obj.getSaleValue());
			ps.setInt(5, obj.isShowed() == true ? 1 : 0);
			ConnectionDatabase.executeUpdate(ps);	
			ResultSet rs=ps.getGeneratedKeys();
            if(rs.next()){
                Integer Id = (Integer) rs.getInt(1);               
                obj.setId(String.valueOf(Id));
            }
			ps.close();			
			
		}catch (Exception e) {
			// TODO: handle exception
			obj = null;
		}
		return obj;
	}
	
	public SaleInfo update(SaleInfo obj){
		try{
			PreparedStatement ps = con.prepareStatement(UPDATE_SQL, PreparedStatement.RETURN_GENERATED_KEYS);			
			ps.setString(1, obj.getName());
			ps.setLong(3, obj.getSaleValue());
			ps.setInt(2, obj.isShowed() == true ? 1 : 0);
			ps.setLong(4, Long.parseLong(obj.getId()));
			ConnectionDatabase.executeUpdate(ps);	
			ps.close();
		}catch (Exception e) {
			// TODO: handle exception
		}
		return obj;
	}
	
	public SaleInfo find(long id){
		List<SaleInfo> saleInfo = new ArrayList<SaleInfo>();
		try{
			PreparedStatement ps = con.prepareStatement(SELECT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,id);
			saleInfo = convert(ConnectionDatabase.executeQuery(ps));
		}catch (Exception e) {
			// TODO: handle exception
		}
		return saleInfo.size()== 0 ? null : saleInfo.get(0);
	}
	
	public List<SaleInfo> findbyProductId(long id){
		List<SaleInfo> saleInfo = new ArrayList<SaleInfo>();
		try{
			PreparedStatement ps = con.prepareStatement(SELECT_BY_PRODUCTID_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,id);
			saleInfo = convert(ConnectionDatabase.executeQuery(ps));
		}catch (Exception e) {
			// TODO: handle exception
			saleInfo = null;
		}
		return saleInfo;
	}
	
	public List<SaleInfo> findAll(){
		List<SaleInfo> saleInfo = new ArrayList<SaleInfo>();
		try{
			saleInfo = convert(ConnectionDatabase.executeQuery(con.prepareStatement(SELECT_ALL_SQL, PreparedStatement.RETURN_GENERATED_KEYS)));
		}catch (Exception e) {
			// TODO: handle exception
			saleInfo = null;
		}
		return saleInfo;
	}
	
	public void delete(SaleInfo obj){
		try{
			
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private List<SaleInfo> convert(ResultSet rs) throws SQLException {
		List<SaleInfo> saleInfo = new ArrayList<SaleInfo>();
		while (rs.next()) {
	        // Get the data from the row using the column index
	        String id = rs.getString(1);
	        String productId = rs.getString(2);
	        String name = rs.getString(3);
	        int value = rs.getInt(5);
	        int show = rs.getInt(4);
	        saleInfo.add(new SaleInfo(id,productId,name,value,show == 1 ? true: false));
	    }
		rs.close();
		return saleInfo;
	}
}
